﻿using Google.Protobuf.WellKnownTypes;
using ManagementApi.Models.System.Menu;
using Modules.System;
using Modules.System.Menu;

namespace ManagementApi.Models.System.Menu
{
    /// <summary>
    /// 菜单视图对象
    /// </summary>
    public class MenuDto
    {
        /// <summary>
        /// 菜单ID
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 父菜单ID
        /// </summary>
        public int ParentId { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string? Name { get; set; }

        /// <summary>
        /// 菜单类型
        /// </summary>
        public string? Type { get; set; }

        /// <summary>
        /// 路由路径
        /// </summary>
        public string? Path { get; set; }

        /// <summary>
        /// 组件路径
        /// </summary>
        public string? Component { get; set; }

        /// <summary>
        /// 菜单排序(数字越小排名越靠前)
        /// </summary>
        public int? Sort { get; set; }

        /// <summary>
        /// "菜单是否可见(1:显示;0:隐藏)
        /// </summary>
        public bool Visible { get; set; }

        /// <summary>
        /// ICON
        /// </summary>
        public string? Icon { get; set; }

        /// <summary>
        /// 跳转路径
        /// </summary>
        public string? Redirect { get; set; }

        /// <summary>
        /// 按钮权限标识
        /// </summary>
        public string? Perm { get; set; }

        /// <summary>
        /// 子菜单
        /// </summary>
        public IEnumerable<MenuDto>? Children { get; set; }
    }
}

/// <summary>
/// 系统菜单局部扩展类
/// </summary>
public static partial class SysMenuExceptions
{
    /// <summary>
    /// 转换为菜单模式
    /// </summary>
    /// <param name="menus"></param>
    /// <param name="keywords"></param>
    /// <returns></returns>
    public static IEnumerable<MenuDto> AsMenus(this IEnumerable<SysMenu> menus, string? keywords)
    {
        if (string.IsNullOrWhiteSpace(keywords))
            return RecurMenus(0, menus);
        else
        {
            IEnumerable<SysMenu> keyMenus = menus.Where(a => a.Name.Contains(keywords));
            var treePath = "";
            foreach (var menu in keyMenus)
            {
                treePath += menu.TreePath + ",";
            }
            if (treePath.Length > 0)
                treePath = treePath.Substring(0, treePath.Length - 1);
            menus = menus.Where(a => treePath.Contains(a.MenuId.ToString())).Union(keyMenus);
            return RecurMenus(0, menus);
        }
    }

    private static IEnumerable<MenuDto> RecurMenus(int parentId, IEnumerable<SysMenu> menus)
    {
        var retMenus = new List<MenuDto>();
        var childrens = menus.Where(m => m.ParentId == parentId);
        foreach (var menu in childrens)
        {
            MenuDto dto = new MenuDto()
            {
                Id = menu.MenuId,
                ParentId = menu.ParentId,
                Name = menu.Name,
                Type = System.Enum.GetName(typeof(MenuType), menu.Type).ToUpper(),
                Path = menu.Path,
                Component = menu.Component,
                Sort = menu.Sort,
                Visible = menu.IsVisible,
                Icon = menu.Icon,
                Redirect = menu.Redirect,
                Perm = menu.Perm,
                Children = RecurMenus(menu.MenuId, menus)
            };
            retMenus.Add(dto);
        }
        return retMenus;
    }
}
